const { ccclass, property } = cc._decorator;
import s from "./AudioManager"
import c from "./PlayerDataSys"
import u from "./AbTestMgr"
import p from "./PeripheralSystemDataMgr"
import d from "./EventMgr"
import h from "./GameEventType"
import f from "./frameworkManager"
import g from "./SdkHelper"
import y from "./EngineUtil"
import m from "./MathUtil"
import _ from "./MatchGameData"
import v from "./BasePageCtrl"
import b from "./PeripheralSystemMgr"
import C from "./UiManage"
import T from "./CashWithDrawPage"
import M from "./WithDrawOptionCtrl"
import BasePageCtrl from "./BasePageCtrl";

@ccclass
export default class CashWithDrawPageCtrl extends BasePageCtrl{
    public P = ["<color=#A15D49>无门槛提现,</color><color=#A15D49>新人登录</color><color=#A15D49>即可按</color><color=#F23712>radio</color><color=#A15D49>提现</color>", "<color=#A15D49>到达第</color><color=#F23712>cityId</color><color=#A15D49>个城市</color><color=#F23712>cityName</color><color=#A15D49>可按</color><color=#F23712>radio</color></color><color=#A15D49>提现，还差<color=#F23712>needCount</color><color=#A15D49>个</color>", "<color=#A15D49>累计打卡</color><color=#F23712>30天</color><color=#A15D49>可按</color><color=#F23712>30%</color><color=#A15D49>提现,</color><color=#A15D49>还差</color><color=#F23712>need</color><color=#A15D49>天</color>"];
    
    public option_index = null;
    public ratio = null;
    public cash_balance = null;
    public real_cash = null;
    public isIncreasePageEnter = false;
    public isGuideEnter = false;
    public fakeBalnace = 0;
    public isGuideExperimentEnter = false;
    public extract_count = 0;

    constructor() {
        var t = this;
        t.ui = null;
        t.option_index = null;
        t.ratio = null;
        t.cash_balance = null;
        t.real_cash = null;
        t.isIncreasePageEnter = false;
        t.isGuideEnter = false;
        t.fakeBalnace = 0;
        t.isGuideExperimentEnter = false;
        t.extract_count = 0;
        return t;
    }
    public onLoad() {
        this.onUILoad();
        this._animType =AnimType.SCALE;
        this._touchControl = false;
        this._hasPeneLock = true;
        this._hasBlack = false;
        this._hasTouchLock = false;
        this._hasBlackTouch = false;
        this._showWinSize = true;
        super.onLoad();
        this.addButtonListen();
        this.addEvent();
        this.dealOppoReview();
    };
    public start() { };
    public dealOppoReview() {
        if (p.isHitOppoReviewBan()) {
            this.ui.instructionsLabel.active = false;
            this.ui.oppoInstructionsLabel.active = true;
        } else {
            this.ui.instructionsLabel.active = true;
            this.ui.oppoInstructionsLabel.active = false;
        }
    };
    public setVisibleArea() {
        this.ui.view.height = this.ui.scrollView.height;
    };
    public addEvent() {
        d.listen(h.WITHDRAW_TIPS_SHOW, this.showWithDrawTips, this);
        d.listen(h.UPDATE_CASH_WITHDRAW, this.updateUserCash, this);
    };
    public removeEvent() {
        d.ignore(h.UPDATE_CASH_WITHDRAW, this.updateUserCash, this);
        d.ignore(h.WITHDRAW_TIPS_SHOW, this.showWithDrawTips, this);
    };
    public updateUserCash() {
        this.setPayMentCanTouch(true);
        this.cash_balance = c.cash_balance;
        this.extract_count = p.getExtractCount();
        var e = {
            option_index: this.option_index,
            ratio: this.ratio
        };
        this.showWithDrawTips(e);
        this.ui.cashLabel.getComponent(cc.Label).string = "我的红包： " + y.getCashBalance(c.cash_balance) + "元";
    };
    public _init(e?) {
        if (e) {
            var t = e.cash_balance, a = e.isIncreasePageEnter, o = e.extract_count, i = e.isGuideEnter, r = e.isGuideExperimentEnter;
            this.cash_balance = t;
            this.isIncreasePageEnter = a;
            this.isGuideEnter = i;
            this.isGuideExperimentEnter = r;
            this.extract_count = o;
            this.ui.cashLabel.getComponent(cc.Label).string = "我的红包： " + y.getCashBalance(this.cash_balance) + "元";
            this.ui.receiptLabel.active = false;
            this.ui.noThresholdTipNode.active = false;
            this.ui.proNode.active = false;
            this.option_index = null;
            this.ratio = null;
            this.real_cash = null;
            this.addOption();
            s.getInstance().playMusic("cash_withDraw_tips");
            if (e.fake_balance && u.getIsLcsmzq()) {
                this.ui.fakeBalanceBg.active = true;
                this.fakeBalnace = e.fake_balance;
            } else {
                this.ui.fakeBalanceBg.active = false;
                this.fakeBalnace = 0;
            }
            this.ui.userID.getComponent(cc.Label).string = "Player ID:" + c.user_id;
        }
    };
    public checkGuide() {
        if (u.isOptimizeTravelFundFlag()) {
            this.setGuideCanTouch(!this.isGuideExperimentEnter);
            this.ui.guide_hand.active = this.isGuideExperimentEnter;
        } else {
            this.setGuideCanTouch(!this.isGuideEnter);
            this.ui.guide_hand.active = this.isGuideEnter;
        }
    };
    public setGuideCanTouch(e) {
        this.ui.btn_close.getComponent(cc.Button).interactable = e;
        this.ui.btn_record.getComponent(cc.Button).interactable = e;
        this.ui.optionNode.children.forEach(function (t) {
            t.getComponent(cc.Button).interactable = e;
        });
    };
    public onUILoad() {
        this.ui = this.node.addComponent(T);
    };
    public addButtonListen() {
       UiManager.addButtonListen(this.ui.btn_close, this.clickClose, this);
       UiManager.addButtonListen(this.ui.btn_weChatPayment, this.clickWeChatPayment, this, 300);
       UiManager.addButtonListen(this.ui.btn_record, this.clickWithDrawRecord, this);
    };
    public clickWeChatPayment() {
        if (null != this.option_index && null != this.ratio) {
            if (this.isGuideEnter || this.isGuideExperimentEnter) {
                p.setOutsideStep(b.OutSideLevelGuide.CASH_WITHDRAW);
                b.reportOutsideGuideStep(b.OutSideLevelGuide.CASH_WITHDRAW);
                this.ui.guide_hand.active = false;
                this.setGuideCanTouch(true);
                g.reportData("guide_wechat_payment");
            }
            if (this.extract_count > 30) {
                y.showManageViewToast("每日30次提现机会已使用完，请明天再来吧");
                f.log("每日30次提现机会已使用完，请明天再来吧");
            } else if (this.real_cash < .1) {
                y.showManageViewToast("提现金额需要满0.1元");
                f.log("提现金额需要满0.1元");
            } else {
                var e = p.getExtractInfo()[this.option_index], t = e.tx_id;
                if (0 == this.option_index) {
                    f.log("无门槛提现============");
                    b.extractCash(t, function () {
                        s.getInstance().playMusic("withdraw_succeed");
                        y.showManageViewToast("提现成功");
                    });
                    this.setPayMentCanTouch(false);
                } else {
                    var a = e.conditions[0], o = e.conditions[1];
                    if (0 == a.status) {
                        var i = a.finish, r = a.need, n = "需要到达" + i + "个城市，还差" + r + "个", l = p.isHitVivoNewUser(), c = p.isHitVivoReviewBan();
                        p.isHitOppoReviewBan();
                        (l || c) && 4 == this.option_index && (n = "到达第21个城市泰山并累计打卡30天可按30%提现，还差" + r + "个城市，" + (u = o.need) + "天打卡");
                        y.showManageViewToast(n);
                        return;
                    }
                    if (0 == o.status) {
                        var u = o.need, d = "累计打卡" + o.finish + "天即可提到微信，还差" + u + "天";
                        y.showManageViewToast(d);
                        return;
                    }
                    if (4 == this.option_index && e.conditions[2] && 0 == e.conditions[2].status) {
                        n = "需要到达" + e.conditions[2].finish + "个城市，还差" + e.conditions[2].need + "个";
                        y.showManageViewToast(n);
                        return;
                    }
                    f.log("满足条件可以提现了发送提现请求============");
                    b.extractCash(t, function () {
                        s.getInstance().playMusic("withdraw_succeed");
                        y.showManageViewToast("提现成功");
                    });
                    this.setPayMentCanTouch(false);
                }
            }
        } else {
            f.log("请选择提现档位");
            y.showManageViewToast("请选择提现档位");
        }
    };
    public setPayMentCanTouch(e) {
        this.ui.btn_weChatPayment.getComponent(cc.Button).interactable = e;
    };
    public clickWithDrawRecord() {
        f.log("点击提现记录");
        b.getExtractCashRecord();
    };
    public clickClose() {
        s.getInstance().stopMusic("cash_withDraw_tips", false);
        this.hide();
        if (this.isIncreasePageEnter || this.isGuideEnter) {
            u.isNoGqtcFlag() ? d.trigger(h.ENTER_GAME_SHOW) : d.trigger(h.SHOW_GAME_START_PAGE, {
                isNeddPop: true
            });
            d.trigger(h.SET_TOP_NODE_CANTOUCH, {
                isCanTouch: true
            });
        }
        this.isGuideExperimentEnter && p.showOutsideGuidePage();
    };
    public addOption() {
        return n(this, void 0, void 0, function () {
            var e, t, a, o, i, r, n;
            return l(this, function (l) {
                switch (l.label) {
                    case 0:
                        this.ui.optionNode.removeAllChildren();
                        e = p.getExtractInfo();
                        return [4,UiManager.loaderPrefab("WithDrawOption")];

                    case 1:
                        t = l.sent();
                        for (a = 0; a < e.length; a++) {
                            o = e[a];
                            i = o.ratio;
                            r = p.getFinshCityIdByIndex(a);
                            (n = cc.instantiate(t)).addComponent(M);
                            n.getComponent(M).initData({
                                city_id: r,
                                radio: i,
                                option_index: a
                            });
                            this.ui.optionNode.addChild(n);
                        }
                        this.scheduleOnce(this.setVisibleArea);
                        this.setDefaultSelect();
                        this.checkGuide();
                        return [2];
                }
            });
        });
    };
    public setDefaultSelect() {
        for (var e = this.ui.optionNode.children, t = p.getDefaultSelectIndex(), a = 0; a < e.length; a++) {
            var o = e[a].getComponent(M);
            o.option_index == t && o.clickOption();
        }
    };
    public showWithDrawTips(e) {
        if (e) {
            var t = e.option_index, a = e.ratio;
            this.option_index = t;
            this.ratio = a;
            var o =MathUtils.accDiv(this.cash_balance, 100);
            u.getIsLcsmzq() && this.fakeBalnace && 5 == t && (o += 1e4);
            o =MathUtils.accMul(o, a);
            var i = y.formatDecimal(o, 2);
            this.real_cash = Number(i);
            this.ui.receiptLabel.active = true;
            this.ui.receiptLabel.getComponent(cc.Label).string = i + "元";
            var r = this.fomatFloat(100 * a, 2) + "%";
            if (0 == this.option_index) {
                this.ui.noThresholdTipNode.active = true;
                this.ui.proNode.active = false;
                this.ui.vivoProTipsLabel.active = false;
                var n = P[0];
                n = n.replace(/radio/g, r);
                this.ui.noThresholdTipNode.getComponent(cc.RichText).string = n;
            } else {
                this.ui.noThresholdTipNode.active = false;
                this.ui.proNode.active = true;
                this.ui.vivoProTipsLabel.active = false;
                var l = p.getExtractInfo()[this.option_index], s = p.getFinshCityIdByIndex(this.option_index), c = p.getCityNameByCityId(s - 1), d = p.isHitOppoReviewBan(), h = u.isDmthFlag();
                u.getDmth2Ab1() ? c = "" : h ? d && (c = "") : _.isOppoChannel() && (c = "");
                var g = l.conditions[0].need, v = l.conditions[0].finish, b = l.conditions[0].current, C = l.conditions[0].status, T = P[1], M = p.isHitVivoReviewBan(), S = p.isHitVivoNewUser(), w = p.isHitOppoReviewBan();
                f.log("命中vivo审核人员=====", M);
                f.log("命中vivo自然渠道新增人员=====", S);
                f.log("命中opoo审核人员=====", w);
                if (4 != this.option_index || 1 != C || M || S || w) if ((M || S || w) && 4 == this.option_index) {
                    var A = "<b><color=#A15D49>依次完成：到达第</color><color=#F23712>cityId</color><color=#A15D49>个城市</color><color=#F23712>cityName</color><color=#A15D49>、累计打卡</color><color=#F23712>30天、</color><color=#A15D49>到达终点可按</color><color=#F23712>30%</color><color=#A15D49>提现。还差</color><color=#F23712>needCount</color><color=#A15D49>个城市到达第21个城市，<color=#F23712>totalNeed</color><color=#A15D49>个城市到达终点，</color><color=#F23712>needDay</color><color=#A15D49>天打卡</color></b>", O = l.conditions[1].need < 0 ? 0 : l.conditions[1].need, I = 32 - b <= 0 ? 0 : 32 - b;
                    g = g < 0 ? 0 : g;
                    A = (A = (A = (A = (A = A.replace(/cityId/g, s.toString())).replace(/cityName/g, c)).replace(/needCount/g, g.toString())).replace(/needDay/g, O.toString())).replace(/totalNeed/g, I.toString());
                    this.ui.vivoProTipsLabel.getComponent(cc.RichText).string = A;
                    this.ui.vivoProTipsLabel.active = true;
                    this.ui.proNode.active = false;
                } else {
                    g = g < 0 ? 0 : g;
                    b = b >= v ? v : b;
                    T = (T = (T = (T = T.replace(/cityId/g, s.toString())).replace(/cityName/g, c)).replace(/radio/g, r)).replace(/needCount/g, g.toString());
                    this.ui.proTipsLabel.getComponent(cc.RichText).string = T;
                    N = b / v, E = y.getProgressWidth(N, 25, 490).width;
                    this.ui.pro.width = E;
                    this.ui.proLabel.getComponent(cc.Label).string = b + "/" + v;
                } else if (0 == l.conditions[1].status) {
                    T = P[2];
                    g = l.conditions[1].need < 0 ? 0 : l.conditions[1].need;
                    b = (b = l.conditions[1].current) >= (v = l.conditions[1].finish) ? v : b;
                    T = T.replace(/need/g, g.toString());
                    this.ui.proTipsLabel.getComponent(cc.RichText).string = T;
                    var N = b / v, E = y.getProgressWidth(N, 25, 490).width;
                    this.ui.pro.width = E;
                    this.ui.proLabel.getComponent(cc.Label).string = b + "/" + v;
                } else {
                    s = l.conditions[2].finish;
                    c = p.getCityNameByCityId(s - 1);
                    u.getDmth2Ab1() ? c = "" : h ? d && (c = "") : _.isOppoChannel() && (c = "");
                    l.conditions[2].need;
                    b = l.conditions[2].current;
                    v = l.conditions[2].finish;
                    g = (g = l.conditions[2].need) < 0 ? 0 : g;
                    b = b >= v ? v : b;
                    T = (T = (T = (T = T.replace(/cityId/g, s.toString())).replace(/cityName/g, c)).replace(/radio/g, r)).replace(/needCount/g, g.toString());
                    this.ui.proTipsLabel.getComponent(cc.RichText).string = T;
                    N = b / v, E = y.getProgressWidth(N, 25, 490).width;
                    this.ui.pro.width = E;
                    this.ui.proLabel.getComponent(cc.Label).string = b + "/" + v;
                }
                this.adjustWidth();
            }
        } else f.log("展示提现提示数据不正确");
    };
    public adjustWidth() {
        var e = this.ui.proTipsLabel.width, t = this.ui.conditionsNode.width, a = 1;
        e >= t - 20 && (a = (t - 20) / e);
        this.ui.proTipsLabel.scale = a;
    };
    public fomatFloat(e, t) {
        return Math.round(e * Math.pow(10, t)) / Math.pow(10, t);
    };
    public update() {
        if (this.ui.fakeBalanceBg.active) {
            var e = y.getNodeAToNodeBPoint(this.ui.cashBg, this.ui.fakeBalanceBg);
            this.ui.fakeBalanceBg.y = e.y + 109;
            this.ui.fakeBalanceBg.y > cc.winSize.height / 2 - 120 ? this.ui.fakeBalanceBg.opacity = 0 : this.ui.fakeBalanceBg.opacity = 255;
        }
    };
    // t.prefabUrl = "CashWithDrawPage";
    // t.className = "CashWithDrawPageCtrl";
    // return r([w, A("UI/pages/CashWithDrawPageCtrl")], t);
}
